<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html>
<html>
<head>
	<title>业务处理-采购订单-订单详情</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-3.3.7-dist/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.min.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/reset.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/style.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath %>css/iframe.css">
</head>
<body>
	<div id="mainContent">
		<div id="mainContent_content">
			<div class="page_header" >
			<a href="<%=basePath %>purchaseOrder/listPurchaseOrder.do">采购订单</a>
			<i class="icon-double-right"></i><a>订单详情</a>
			</div>
			<div class="btn rightBtn">
				<c:if test="${isEdit && order.status==0 }">
					<input type="button" class="btn btn1" value="新增物品" onclick="toAddOrderItem();"></input>
					<input type="button" class="btn btn2" value="保存" onclick="savePurchaseOrder();"></input>
					<input type="button" class="btn btn1" value="提交" onclick="saveAndSubmit();"></input>
				</c:if>
				<c:if test="${isEdit && order.status==3 && order.isSettle==0}">
					<input type="button" class="btn btn1" value="结算" onclick="confirmSettlePurchaseOrder(${order.purchaseOrderID });"></input>
				</c:if>
				<input type="button" class="btn btn2" value="下载PDF" onclick="window.open('<%=basePath %>purchaseOrder/download.do?purchaseOrderID=${order.purchaseOrderID }');"></input>
				<input type="button" class="btn btn2" value="返回" onclick="location.href='<%=basePath %>purchaseOrder/listPurchaseOrder.do'"></input>
			</div>
			<form id="updatePurchaseOrderForm" class="form">
			<div id="requisitionForm" class="box">
				<input type="hidden" name="purchaseOrderID" value="${order.purchaseOrderID }">
				<p>
					<label>订购单号</label>
					<input type="text" class="form-control" readonly value="${order.orderNumber }"></input>
					<label>采购人</label>
					<input type="text" class="form-control" readonly value="${order.submitterName }"></input>
					<label>状态</label>
					<c:if test="${order.status==0 }"><input readonly type="text" class="form-control" value="未提交"></c:if>
					<c:if test="${order.status==1 }"><input readonly type="text" class="form-control" value="已提交"></c:if>
					<c:if test="${order.status==2 }"><input readonly type="text" class="form-control" value="审核中"></c:if>
					<c:if test="${order.status==3 }"><input readonly type="text" class="form-control" value="已通过"></c:if>
					<c:if test="${order.status==4 }"><input readonly type="text" class="form-control" value="未通过"></c:if>
					<label>应付货款</label>
					<input type="text" class="form-control" readonly value="${order.shouldPay }"></input>
				</p>	
				<p>
					<label>仓库</label>
					<select name="warehousemanagementid" onchange="clearItem();" class="form-control select" <c:if test="${!isEdit }">disabled</c:if>>
						<option value=""></option>
						<c:forEach items="${warehouseList }" var="warehouse">
							<option value="${warehouse.warehousemanagementid }" <c:if test="${order.warehousemanagementid==warehouse.warehousemanagementid}">selected</c:if>>${warehouse.warehousename }</option>
						</c:forEach>
					</select>
					<label>供应商</label>
					<select name="supplierID" onchange="clearItem();" class="form-control select" <c:if test="${!isEdit }">disabled</c:if>>
						<option value=""></option>
						<c:forEach items="${supplierList }" var="supplier">
							<option value="${supplier.supplierid }" <c:if test="${order.supplierID==supplier.supplierid}">selected</c:if>>${supplier.chinesename }</option>
						</c:forEach>
					</select>
					<label>类型</label>
					<select name="materialMaterial" onchange="clearItem();" class="form-control select" <c:if test="${!isEdit }">disabled</c:if>>
						<option value="普通物料" <c:if test="${order.materialMaterial=='普通物料'}">selected</c:if>>普通物料</option>
						<option value="IMPA物料" <c:if test="${order.materialMaterial=='IMPA物料'}">selected</c:if>>IMPA物料</option>
					</select>
					<label>实付货款</label>
					<input type="text" class="form-control" readonly value="${order.actualPay }"></input>
				</p>
				<p>
					<label>备注</label>
					<input type="text" class="form-control maxWidth" value="${order.remarks }" <c:if test="${!isEdit }">readonly</c:if>></input>
				</p>
			</div>
			<div class="tableBox box">
				<table class="table table-bordered table_text_center">
					<thead>
						<tr>
							<th>物品编码</th>
							<th>物品名称</th>
							<th>单位</th>
							<th>采购数量</th>
							<th>小计</th>
							<c:if test="${isEdit && order.status==0 }">
								<th>操作</th>
							</c:if>
						</tr>
					</thead>
					<tbody id="purchaseOrderItemTbody">
					<c:forEach items="${itemList }" var="item">
						<tr>
							<td>${item.itemCode }</td>
							<td>${item.chineseName }</td>
							<td>${item.purchaseSpecifications}${item.salesUnit }/${item.purchasingUnit }</td>
							<c:if test="${isEdit && order.status==0 }">
								<td><input type='text' class='form-control' value='<fmt:formatNumber value="${item.number/item.purchaseSpecifications }" pattern="#0"/>' data-purchaseSpecifications='${item.purchaseSpecifications }' onkeyup='limitInputNumber(this);countItem(this);' onchange='limitInputNumber(this);countItem(this);'/></td>
								<td><input type='text' class='form-control' name='itemNumber' value="${item.number }" readonly ></td>
								<td><a class="btn btn-xs btn_danger" onclick='deleteItem(this);'>删除</a><input type='hidden' name='sourceID' value='${item.sourceID }' /></td>
							</c:if>
							<c:if test="${!isEdit || order.status!=0 }">
								<td><fmt:formatNumber value="${item.number/item.purchaseSpecifications }" pattern="#0"/></td>
								<td>${item.number }</td>
							</c:if>
						</tr>
					</c:forEach>
					</tbody>
				</table>
			</div>
			<div class="box updateBox">
			<p>
				<label>创建时间</label>
				<input type="text" class="form-control" readonly value="<fmt:formatDate value='${order.createDate }' type='date'/>"></input>
				<label>最后处理人</label>
				<input type="text" class="form-control" readonly value="${order.lastModifiedPerson }"></input>
				<label>最后处理时间</label>
				<input type="text" class="form-control" readonly value="<fmt:formatDate value='${order.lastModifiedTime }' type='date'/>"></input>
			</p>
			</div>
			</form>
		</div>
		<jsp:include page="/jsp/business/purchaseOrder/addPurchaseOrderItem.jsp"></jsp:include>
		<jsp:include page="/jsp/business/purchaseOrder/settle.jsp"></jsp:include>
		<jsp:include page="/jsp/basic/prompt.jsp"></jsp:include>
	</div>
	<script src="<%=basePath %>js/jquery-3.2.1.min.js"></script>
	<script src="<%=basePath %>libs/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/bootstrap-table.js"></script>
	<script src="<%=basePath %>libs/bootstrap-table-master/dist/locale/bootstrap-table-zh-CN.min.js"></script>
	<script src="<%=basePath %>My97DatePickers/WdatePicker.js"></script>
	<script src="<%=basePath %>js/public.js"></script>
	<script type="text/javascript">
	function addToHasAddTable(item){
		var html="<tr>";
		html+="<td>"+item.itemCode+"</td>";
		html+="<td>"+item.chineseName+"</td>";
		html+="<td>"+item.purchaseSpecifications+item.salesUnit+"/"+item.purchasingUnit+"</td>";
		html+="<td><input type='text' class='form-control' value='"+item.number/item.purchaseSpecifications+"' data-purchaseSpecifications='"+item.purchaseSpecifications+"' onkeyup='limitInputNumber(this);countItem(this);' onchange='limitInputNumber(this);countItem(this);'/></td>";
		html+="<td><input type='text' class='form-control' name='itemNumber' value='"+item.number+"' readonly ></td>";
		html+="<td><a class='btn btn-xs btn_danger' onclick='deleteItem(this);'>删除</a>";
		html+="<input type='hidden' name='sourceID' value='"+item.id+"' /></td>";
		html+="</tr>";
		$("#purchaseOrderItemTbody").append(html);
	}		
	
	function countItem(obj){
		var numInput=$(obj).parent().parent().find("input[name='itemNumber']");
		numInput.val("0");
		var num=$(obj).val()*$(obj).attr("data-purchaseSpecifications");
		numInput.val(num);
	}
	
	function deleteItem(obj){
		$(obj).parent().parent().remove();
	}
	
	function clearItem(){
		$("#purchaseOrderItemTbody").html("");
	}
	
	function validateForm(){
		var length=$("#purchaseOrderItemTbody [name='sourceID']").length;
		if(!length>0){
			Prompt.alert("提示","订购单物品种类数量必须大于0！")
			return false;
		}
		var status=$("#updatePurchaseOrderForm [name='status']").val();
		var supplierId=$("#updatePurchaseOrderForm [name='supplierId']").val();
		if(supplierId==''){
			Prompt.alert("提示","请选择供应商！")
			return false;
		}
		var warehousemanagementid=$("#updatePurchaseOrderForm [name='warehousemanagementid']").val();
		if(warehousemanagementid==''){
			Prompt.alert("提示","请选择仓库！")
			return false;
		}
		return true;
	}
	
	function savePurchaseOrder(){
		$.ajax({
			url:"<%=basePath %>purchaseOrder/update.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#updatePurchaseOrderForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购订单保存成功！",function(){
						location.href="<%=basePath %>purchaseOrder/listPurchaseOrder.do";
					});
				}else{
					Prompt.alert("提示","采购订单保存失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购订单保存失败！");
			}
		});
	}
	
	function saveAndSubmit(){
		if(!validateForm()) return;
		$.ajax({
			url:"<%=basePath %>purchaseOrder/updateAndSubmit.do",
			type:"post",
			async : false,
			dataType:"json",
			data : $("#updatePurchaseOrderForm").serialize(),
			success:function(data){
				if(data.result=="success"){
					Prompt.alert("提示","采购订单提交成功！",function(){
						location.href="<%=basePath %>purchaseOrder/listPurchaseOrder.do";
					});
				}else if(data.result=="submitFail"){
					Prompt.alert("提示","采购订单已成功！提交失败",Prompt.refresh);
				}else{
					Prompt.alert("提示","采购订单提交失败！");
				}
			},
			error :function(){
				Prompt.alert("提示","采购订单提交失败！");
			}
		});
	}
	</script>
</body>
</html>